/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generation;

import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.base.dao.DAOException;
import com.farmcode.console.api.business.custom.ExtPropertyManager;
import com.farmcode.console.api.model.Project;
import com.farmcode.console.api.model.Property;
import com.farmcode.web.BaseFARMCodeBacking;
import com.farmcode.web.generation.objectinfo.ProcedureBackingBean;
import com.farmcode.web.generation.objectinfo.TablesBackingBean;
import com.farmcode.web.generation.objectinfo.ViewsBackingBean;
import com.farmcode.web.generation.template.TemplateBackingBean;
import com.farmcode.web.project.DatasourceBackingBean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;

/**
 *
 * @author Oscar Ivan Prado
 */
public class BaseCodeGenerator extends BaseFARMCodeBacking {

    
    public static final String PROJECTS = BaseCodeGenerator.class.getName() + ".projects";
    
    private boolean enabled;
    public static final String GO_GENERATE = "goGenerateManual";
    private String projectName;
    @EJB
    private ExtPropertyManager propertyManager;
    
    public void refresh(){
        //Restaura la información referente a los templates
        getSession().setValue(TemplateBackingBean.TEMPLATES_SESSION_NAME, null);
        //Restaura la información referente a las tablas
        getSession().setValue(TablesBackingBean.OBJECTS_INFO, null);
        getSession().setValue(TablesBackingBean.CURRENT_OBJECT_INFO, null);
        getSession().setValue(TablesBackingBean.CURRENT_ATTIRBUTES_SELECTED, null);
        //Restaura la información referente a las vistas
        getSession().setValue(ViewsBackingBean.OBJECTS_INFO, null);
        getSession().setValue(ViewsBackingBean.CURRENT_OBJECT_INFO, null);
        getSession().setValue(ViewsBackingBean.CURRENT_ATTIRBUTES_SELECTED, null);
        //Restaura la información referente a los procedimientos
        getSession().setValue(ProcedureBackingBean.OBJECTS_INFO, null);
        getSession().setValue(ProcedureBackingBean.CURRENT_OBJECT_INFO, null);
        getSession().setValue(ProcedureBackingBean.CURRENT_ATTIRBUTES_SELECTED, null);
        //Restaura la información de los templates
        getSession().setValue(TemplateBackingBean.TEMPLATES_SESSION_NAME, null);
    }
    
    public String goGenerateManual(){
        setProject(null);
        refresh();
        getSession().setValue(PROJECTS, null);
        return GO_GENERATE;
    }
    
    public String goGenerateProject(Project project){        
        try {
            refresh();
            setProject(project);
            newContext(); 
            
            Property property = propertyManager.getPropertyByName(project, FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER);
            getSession().setValue(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER, property.getValue());

            if(property.getValue().equals(DatasourceBackingBean.JDBC_DATABASE_EXPLORER)){

                String driver = propertyManager.getPropertyByName(project, FARMCodeContext.DRIVER).getValue();
                String host = propertyManager.getPropertyByName(project, FARMCodeContext.HOST).getValue();
                String user = propertyManager.getPropertyByName(project, FARMCodeContext.USER).getValue();
                String password = propertyManager.getPropertyByName(project, FARMCodeContext.PASSWORD).getValue();
                String schema  = propertyManager.getPropertyByName(project, FARMCodeContext.SCHEMA).getValue();

                getSession().setValue(DatasourceBackingBean.JDBC_DATABASE_EXPLORER_SELECTED, true);
                getSession().setValue(FARMCodeContext.DRIVER, driver);
                getSession().setValue(FARMCodeContext.HOST, host);
                getSession().setValue(FARMCodeContext.USER, user);
                getSession().setValue(FARMCodeContext.PASSWORD, password);
                getSession().setValue(FARMCodeContext.SCHEMA, schema);
            }else{
                getSession().setValue(DatasourceBackingBean.JDBC_DATABASE_EXPLORER_SELECTED, false);
            }
                
            getSession().setValue(PROJECTS, null);
            
        } catch (DAOException ex) {
            Logger.getLogger(BaseCodeGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return GO_GENERATE;
    }
    
    /**
     * @return the enabled
     */
    public boolean isEnabled() {
        enabled = getContext().size() > 0;
        return enabled;
    }

    /**
     * @param enabled the enabled to set
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}
